/*
  ! async 函数是什么？一句话，它就是 Generator 函数的语法糖。
  !（1）内置执行器。
  !（2）更好的语义。
  !（3）更广的适用性。
  !（4）返回值是 Promise
*/

const fs = require("fs");
const read = (url) => {
  return new Promise((resolve) => {
    fs.readFile(url, "utf-8", (err, data) => {
      if (err) throw err;
      resolve(data);
    });
  });
};

const delay = (time) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
};

const fn = async () => {
  await delay(2000);
  const res = await read("./1.txt");
  console.log(res);
  const res2 = await read("./2.txt");
  console.log(res2);
};

fn();

// async需要处理错误,使用try catch
